define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var arrayBufferHelp = require("$UI/highde/modbusItems/lib/arrayBufferHelp");

	require("$UI/system/lib/cordova/cordova");
	require("cordova!cordova-plugin-chrome-apps-sockets-tcp");
	var netTcpHelp = {
		g_hasConnected : false,
		socketId : -1,
		hasListener : false,
		isSendding : false,
		isConnectting : false,
		SendDataChrome : function(ip,port,content){
			if(typeof port !== 'number' || port%1 !== 0 || port < 1 || port > 65535){
				alert("端口设置不合理");
				return;
			}
			//JSON.stringify(object);
			var self = this;
			if(self.socketId < 0){
				chrome.sockets.tcp.create(function(info){
					self.socketId = info.socketId;
					self.g_hasConnected = false;
					self.SendDataChrome(ip,port,content);
				});
			}
			else{
				if(self.g_hasConnected == false)
					self.t_connect(ip,port,content);
				else {
					chrome.sockets.tcp.getInfo(self.socketId, function(socketInfo){
						//alert("get info");
						if(socketInfo.connected != true){
							chrome.sockets.tcp.disconnect(self.socketId, function(){});
							//alert("connect failed");
							self.socketId = -1;
							self.hasListener = false;
							self.g_hasConnected = false;
							self.t_connect(ip,port,content);
						}
						else{
							//chrome.sockets.tcp.setKeepAlive(socketId, false,0,function(result){
								//alert(JSON.stringify(result));
							//});

							if(self.isSendding)
								return;
							else
								self.isSendding = true;
							var buf = arrayBufferHelp.str2ab(content);
							chrome.sockets.tcp.send(self.socketId, buf, function(sendInfo){
								self.isSendding = false;
								//chrome.sockets.tcp.disconnect(socketId, function(){
									//alert("disConnet");
								//});
								//chrome.sockets.tcp.close(socketId, function(){alert("closed");})
								//alert(JSON.stringify(sendInfo));
							});
						}
					});
				}
			}
		},

		t_connect : function (ip,port,content){
			if(typeof port !== 'number' || port%1 !== 0 || port < 1 || port > 65535){
				alert("端口设置不合理");
				return;
			}
			var self = this;
			if(self.isConnectting == true){
				return;
			}
			try{
				self.isConnectting = true;
				chrome.sockets.tcp.connect(self.socketId, ip, port, function(result){
					if(result < 0){
						chrome.sockets.tcp.close(self.socketId, function(){
							self.socketId = -1;
						});							
						self.g_hasConnected = false;
					}						
					else{
						self.g_hasConnected = true;
						chrome.sockets.tcp.onReceive.addListener(function(info){
							if(typeof self.ReceiveCallBack == 'function'){
								self.ReceiveCallBack(info);
							}
						});
						self.hasListener = true;
						self.SendDataChrome(ip,port,content);
					}
					self.isConnectting = false;
				});
			}
			catch(err){ 
				alert(err);
			}
			finally{ 
				//isConnectting = false;
			}
		},
		
		ReceiveCallBack : null,
		
		SetReceiveCallBack : function(fun){
			if(typeof fun == 'function')
				this.ReceiveCallBack = fun;
		}
	};
	
	
	
	return netTcpHelp;
});